//
//  CountDownControl.m
//  CountDown
//
//  Created by mmac on 11/1/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CountDownController.h"



@implementation CountDownController

@synthesize cdTimer;
@synthesize label;
@synthesize progress;
@synthesize cdCount;
@synthesize cdCountTotal;

- (IBAction)startTimer:sender {
	
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2
			target:self 
			selector:@selector(cdTimerFireMethod:)
			userInfo:nil
			repeats:YES];
    self.cdTimer = timer;
}

- (IBAction)stopTimer:sender {
    [cdTimer invalidate];
    self.cdTimer = nil;
}

- (void)cdTimerFireMethod:(NSTimer*)theTimer {
	self.cdCount++;
	[self showCdCount];
}


- (IBAction)resetCdCount{
	self.cdCount = 0;
	[self showCdCount];
}

- (void)showCdCount{
    NSString *txt = [[NSString alloc] initWithFormat:@"%u", self.cdCount];
    label.text = txt;
    [txt release];	
	
	float pg;
	pg = (float)( self.cdCountTotal - self.cdCount ) / (float)(self.cdCountTotal);
	NSLog( @"pg %f", pg );
	if(pg < 0.0 || pg > 1.0){
		pg = 0.0;
	}
	progress.progress = pg;
	
	NSLog( @"Count %u, %f", self.cdCount, pg );
}


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	
	self.cdCount = 0;
	self.cdCountTotal = 120;
	
	[self showCdCount];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	if( self.cdTimer != nil){
		[cdTimer invalidate];
		self.cdTimer = nil;
	}
   
}


- (void)dealloc {
    [super dealloc];
}


@end
